package com.mongo.demo;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.BasicBSONObject;
import org.bson.Document;
import org.junit.Test;
import util.IdWorker;

import java.util.HashMap;
import java.util.Map;

public class MongoTest {
    //查询所有的吐槽记录
    @Test
    public void test01(){
        //得到一个mongodb对象
        MongoClient mongoClient=new MongoClient("192.168.4.155");
        //根据mongodbclinet得到一个数据库对象
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //从数据库中得到集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //遍历集合得到每个文档(行)
        FindIterable<Document> documents = spit.find();
        printinfo(documents);
    }



    //打印输出
    public static void printinfo(FindIterable<Document> documents){
        //遍历并打印文档对象
        for (Document document : documents) {
            //5.1）输出文档对象的内容
            Object id = document.get("_id");
            String content = document.getString("content");
            String userid = document.getString("userid");
            String nickname = document.getString("nickname");
            Integer visits = document.getInteger("visits");
            System.out.println("id:" + id);
            System.out.println("content:" + content);
            System.out.println("userid:" + userid);
            System.out.println("nickname:" + nickname);
            System.out.println("visits:" + visits);
            System.out.println("---------------------------------------------");
        }
    }
    //条件查询：
    @Test
    public void test02(){
            //
        MongoClient mongoClient=new MongoClient("192.168.4.155");
        //
        MongoDatabase database = mongoClient.getDatabase("spitdb");
        //得到集合
        MongoCollection<Document> spit = database.getCollection("spit");
        //按照条件查询
        BasicDBObject bson=new BasicDBObject("userid","1013");
        //得到文档
        FindIterable<Document> documents = spit.find(bson);
        printinfo(documents);
    }
    //查询浏览量大于1000的记录
    @Test
    public void test03(){
        //
        MongoClient mongoClient=new MongoClient("192.168.4.155");
        //
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //
        BasicDBObject object=new BasicDBObject("visits",new BasicDBObject("$gt",1000));
        //
        FindIterable<Document> documents = spit.find(object);
        //
        printinfo(documents);
    }
    //插入数据
    @Test
    public void test04(){
        //
        MongoClient mongoClient=new MongoClient("192.168.4.155");
        //
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //
        Map<String,Object> map=new HashMap<>();
        //
        IdWorker idWorker=new IdWorker();
        map.put("_id","111");
        map.put("content","你不懂");
        map.put("userid","1020");
        map.put("nickname","小王");
        map.put("visits",3000);
        Document document=new Document(map);
        spit.insertOne(document);
        mongoClient.close();
    }



}
